﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using System.Reflection;
using Newtonsoft.Json;
using IPR_StudyMeetingMrgSys.Models;
using System.Web;
using System.IO;
using Aspose;
using Aspose.Words;
using IPR_StudyMeetingMrgSys.Model;
using IPR_StudyMeetingMrgSys.BLL;
using System.Security.Cryptography;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class FileUploadController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        //[HttpPost]
        //[Route("api/FileUpload/Upload")]
        //public IHttpActionResult FileUpload()
        //{
        //    Res res = new Res();
        //    var FTID = HttpContext.Current.Request["FTID"].ToString();
        //    var KKID = HttpContext.Current.Request["KKID"].ToString();
        //    var TFUploadDeptID = HttpContext.Current.Request["TFUploadDeptID"].ToString();
        //    var TFFileLevel = HttpContext.Current.Request["TFFileLevel"].ToString();
        //    var TFFileKey = HttpContext.Current.Request["TFFileKey"].ToString();
        //    var TFReadTime = HttpContext.Current.Request["TFReadTime"].ToString()==""?"0": HttpContext.Current.Request["TFReadTime"].ToString();

        //    var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;

        //    var path = "";
        //    var fileName = "";
        //    if (file != null && file.ContentLength > 0)
        //    {
        //        fileName = Path.GetFileName(file.FileName);

        //        path = Path.Combine(
        //            HttpContext.Current.Server.MapPath("~/uploads"),
        //            fileName
        //        );

        //        //file.SaveAs(path);
        //        #region 开始保存
        //        string sExt = System.IO.Path.GetExtension(fileName);
        //        string svrFilePathPlusName = "";
        //        string sFactPathPDF = ""; 
        //        if (sExt.ToUpper() == ".PDF")
        //        {
        //            //如果是PDF文件直接存储
        //            file.SaveAs(path);
        //            svrFilePathPlusName = "/uploads/" + fileName;
        //        }
        //        else if (sExt.ToUpper() == ".DOC" || sExt.ToUpper() == ".DOCX")
        //        {
        //            //如果是word文件需要转换为pdf文件后，同时名称改变
        //            string strDt = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        //            string path1 = Path.Combine(
        //            HttpContext.Current.Server.MapPath("~/uploaddocs"),
        //            strDt + fileName
        //            );

        //            file.SaveAs(path1);

        //            Document doc = new Document(path1);
        //            path = Path.Combine(
        //                HttpContext.Current.Server.MapPath("~/uploads"),
        //                strDt+".pdf"
        //            );
        //            sFactPathPDF = path;
        //            doc.Save(sFactPathPDF, Aspose.Words.SaveFormat.Pdf);
        //            svrFilePathPlusName = "/uploads/" + strDt + ".PDF";
        //        }
        //        else 
        //        {
        //            //图片或者视频等其它文件直接存储
        //            file.SaveAs(path);
        //            svrFilePathPlusName = "/uploads/" + fileName;
        //        }
        //        #endregion
        //        #region 汇总数据字段
        //        try
        //        {
        //            Model.TrainFiles mTF = new Model.TrainFiles();
        //            mTF.TFName = fileName.Split('.')[0];
        //            mTF.KKID = Convert.ToInt32(KKID);
        //            mTF.FTID = Convert.ToInt32(FTID);
        //            mTF.TFUploadDeptID = Convert.ToInt32(TFUploadDeptID);
        //            mTF.TFFileLevel = TFFileLevel;
        //            mTF.TFFileKey = TFFileKey;
        //            mTF.TFReadTime = Convert.ToInt32(TFReadTime);
        //            mTF.TFPath = path;
        //            mTF.TFSize = file.ContentLength / 1024;
        //            mTF.TFMD5 = MD5Encrypt(fileName, "ZZIPRYFB");
        //            mTF.TFUseCount = 0;
        //            mTF.TFIsPhoto = ".jpg|.png|.jpeg|.bmp|.gif".Contains(sExt.ToLower()) ? 1 : 0;
        //            mTF.TFIsPass = 0;
        //            mTF.TFAddUserID = LoginUser.UserInfo.EID;
        //            mTF.TFAddUserName = LoginUser.UserInfo.EmpName;
        //            mTF.TFAddDateTime = DateTime.Now;
        //            mTF.TFServerPath = svrFilePathPlusName;
        //            mTF.TFIsUse = 1;

        //            bool blExist = new BLL.TrainFiles().Exists(mTF.TFName);
        //            if (blExist)
        //            {
        //                //如果已经存在，说明上传重复，不处理
        //            }
        //            else
        //            {
        //                new BLL.TrainFiles().Add(mTF);
        //            }
        //            res.code = (int)StatusCodeEnum.FileUploadSucess;
        //            res.msg = StatusCodeEnum.FileUploadSucess.GetEnumText();
        //            res.data = new cdata();
        //            res.data.src = "/uploads/" + file.FileName;
        //        }
        //        catch(Exception exc)
        //        {
        //            res.code = (int)StatusCodeEnum.FileUploadSucess;
        //            res.msg = exc.Message;
        //            res.data = new cdata();
        //            res.data.src = "/uploads/" + file.FileName;
        //        }
        //        #endregion 
        //    }
        //    log.Info( "文件数"+ HttpContext.Current.Request.Files.Count.ToString()+"="+ fileName + "-" + path);
        //    return Json(res);
        //}
        //public string MD5Encrypt(string pToEncrypt, string sKey)
        //{
        //    MD5 md5 = new MD5CryptoServiceProvider();
        //    byte[] fromData = System.Text.Encoding.Unicode.GetBytes(pToEncrypt);
        //    byte[] targetData = md5.ComputeHash(fromData);
        //    string byte2String = null;

        //    for (int i = 0; i < targetData.Length; i++)
        //    {
        //        byte2String += targetData[i].ToString("X");
        //    }

        //    return byte2String;
        //}
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

    public class Res
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int code { get; set; }

        /// <summary>
        /// 操作信息
        /// </summary>
        public string msg { get; set; }

        private cdata mydata;
        /// <summary>
        /// 返回数据
        /// </summary>
        public cdata data
        {
            get { return mydata; }
            set { mydata = value; }
        }
    }
    public class cdata
    {   
        /// <summary>
         /// 操作信息
         /// </summary>
        public string src { get; set; }
    }

}